package org.OpenGeoPortal.JUnit;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.OpenGeoPortal.Solr.Searcher;
import org.junit.Test;
import junit.framework.Assert;

/**
 * code to test the map/layer intersection code
 * This code assumes the intersection test layers have been loaded into Solr.
 * The test layers assume the lower left corner of the map is (20, 20)
 *   and the upper right is (40, 40).
 * There are 14 test layers.  4 of them intersection map corners.  4 of them
 *   intersect along the sides of the map.  One layer is contained within the
 *   map and another completely covers the map.  Finally, 4 others do not intersect
 *   the map.  They are either completely above, below, to the left or to the right 
 *   of the map.
 * 
 * @author smcdon08
 *
 */
public class SearcherTest 
{

	/**
	 * Are only the expected layers intersecting the map?
	 * Generate a Solr query, send it to Solr and check the results
	 */
	@Test
	public void intersectionTest()
	{
		String server = "localhost";
	 	int port = 8480;
	 	Searcher searcher = new Searcher(20, 40, 20, 40);
	 	String solrQuery = searcher.geoFilter();
		String intersectionFilter = searcher.layerIntersectsMapFilter();
		
		String url$ = "http://" + server + ":" + port + "/solr/select/?q=" + solrQuery + "&debugQuery=off&version=2.2"
		+ "&rows=20&indent=on&wt=json";
		url$ = url$ + "&fq=" + intersectionFilter;
		System.out.println(" in SearcherTest.intersectionTest, url = " + url$);
		try
		{
			URL url = new URL(url$);
			InputStream inputStream = url.openStream();
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
			BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
			String currentLine = bufferedReader.readLine();
			String json = "";
			while (currentLine != null)
			{
				currentLine = currentLine.replaceAll("\\n|\\r", " ");
				json = json + currentLine;
				if (currentLine.contains("LayerDisplayName"))
					System.out.println(currentLine);	
				currentLine = bufferedReader.readLine();
			}
			// verify contents of search results
			Assert.assertTrue("Inner Layer not found", json.contains("Inner Layer"));
			Assert.assertTrue("Outer Layer not found", json.contains("Outer Layer"));
			Assert.assertTrue("Intersection Layer Lower Left not found", json.contains("Intersection Layer Lower Left"));
			Assert.assertTrue("Intersection Layer Lower Right not found", json.contains("Intersection Layer Lower Right"));
			Assert.assertTrue("Intersection Layer Upper Left not found", json.contains("Intersection Layer Upper Left"));
			Assert.assertTrue("Intersection Layer Upper Right not found", json.contains("Intersection Layer Upper Right"));
			Assert.assertTrue("Intersection Layer Left not found", json.contains("Intersection Layer Left"));
			Assert.assertTrue("Intersection Layer Right not found", json.contains("Intersection Layer Right"));
			Assert.assertTrue("Intersection Layer Upper not found", json.contains("Intersection Layer Upper"));
			Assert.assertTrue("Intersection Layer Lower not found", json.contains("Intersection Layer Lower"));
			
			Assert.assertFalse("Left Layer not expected", json.contains("Left Layer"));
			Assert.assertFalse("Right Layer not expected", json.contains("Right Layer"));
			Assert.assertFalse("Upper Layer not expected", json.contains("Upper Layer"));
			Assert.assertFalse("Lower Layer not expected", json.contains("Lower Layer"));
		}
		catch (MalformedURLException e)
		{
			System.out.println("bad url: " + url$);
			Assert.assertTrue("URL error", false);
		}
		catch (IOException e) 
		{
			System.out.println("io exception " + e);
			Assert.assertTrue("IOException", false);
		}
	}
}
